/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.product;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProductComment;
import com.thinkgem.jeesite.modules.hk.shop.dao.product.HkShopProductCommentDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProductCommentImg;
import com.thinkgem.jeesite.modules.hk.shop.dao.product.HkShopProductCommentImgDao;

/**
 * 乐购产品评价Service
 * @author 罗天文
 * @version 2018-05-19
 */
@Service
@Transactional(readOnly = true)
public class HkShopProductCommentService extends CrudService<HkShopProductCommentDao, HkShopProductComment> {

	@Autowired
	private HkShopProductCommentImgDao hkShopProductCommentImgDao;
	
	public HkShopProductComment get(String id) {
		HkShopProductComment hkShopProductComment = super.get(id);
		hkShopProductComment.setHkShopProductCommentImgList(hkShopProductCommentImgDao.findList(new HkShopProductCommentImg(hkShopProductComment)));
		return hkShopProductComment;
	}
	
	public List<HkShopProductComment> findList(HkShopProductComment hkShopProductComment) {
		return super.findList(hkShopProductComment);
	}
	
	public Page<HkShopProductComment> findPage(Page<HkShopProductComment> page, HkShopProductComment hkShopProductComment) {
		return super.findPage(page, hkShopProductComment);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopProductComment hkShopProductComment) {
		super.save(hkShopProductComment);
		for (HkShopProductCommentImg hkShopProductCommentImg : hkShopProductComment.getHkShopProductCommentImgList()){
			if (hkShopProductCommentImg.getId() == null){
				continue;
			}
			if (HkShopProductCommentImg.DEL_FLAG_NORMAL.equals(hkShopProductCommentImg.getDelFlag())){
				if (StringUtils.isBlank(hkShopProductCommentImg.getId())){
					hkShopProductCommentImg.setComment(hkShopProductComment);
					hkShopProductCommentImg.preInsert();
					hkShopProductCommentImgDao.insert(hkShopProductCommentImg);
				}else{
					hkShopProductCommentImg.preUpdate();
					hkShopProductCommentImgDao.update(hkShopProductCommentImg);
				}
			}else{
				hkShopProductCommentImgDao.delete(hkShopProductCommentImg);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopProductComment hkShopProductComment) {
		super.delete(hkShopProductComment);
		hkShopProductCommentImgDao.delete(new HkShopProductCommentImg(hkShopProductComment));
	}
	
}